//
//  EWELineView.swift
//  Auscultation
//
//  Created by 来 on 2020/12/18.
//

import Foundation

class EWELineView: UIView {
    
    var back: UIView!
    var lastPoint: CGPoint!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        stop()
    }
    
    deinit {
        stop()
    }
    
    func animationWidth(_ duration: CFTimeInterval, _ from: CGFloat, _ to: CGFloat) -> CABasicAnimation {
        let animation = CABasicAnimation(keyPath: "transform.translation.x")
        animation.duration = duration
        animation.beginTime = 0
        animation.fromValue = from
        animation.toValue = to
        animation.timingFunction = CAMediaTimingFunction(name: .linear)
        animation.autoreverses = false
        animation.isRemovedOnCompletion = false
        animation.fillMode = .forwards
        return animation
    }
    
    func pauseAnimate() {
        DispatchQueue.main.async { [weak self] in
            if self == nil {
                return
            }
            let pausedTime: CFTimeInterval = (self?.layer.convertTime(CACurrentMediaTime(), from: nil))!
            self?.layer.speed = 0.0;
            self?.layer.timeOffset = pausedTime;
        }
    }
    
    func resumeAnimate() {
        DispatchQueue.main.async { [weak self] in
            if self == nil {
                return
            }
            let pausedTime = self?.layer.timeOffset
            self?.layer.speed = 1.0;
            self?.layer.timeOffset = 0.0;
            self?.layer.beginTime = 0.0;
            let timeSincePause = (self?.layer.convertTime(CACurrentMediaTime(), from: nil))! - pausedTime!;
            self?.layer.beginTime = timeSincePause;
        }
    }
    
    func add_back_view(_ nums: [Double], _ frame: CGRect) {
        let back_view = LineBackView.init(frame: frame)
        back_view.backgroundColor = UIColor.clear
        back.addSubview(back_view)
        back_view.show(nums)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getNumStrs(_ numStrs: [String]) -> [String] {
        var nnumStrs: [String] = []
        for (si, v) in numStrs.enumerated() {
            if si % 2 == 0 {
                nnumStrs.append(v)
            }
        }
        return nnumStrs
    }
    
    func start() {
        self.back.layer.add(self.animationWidth(150000, 0, -(lgf_Width * 100000.0)), forKey: "LineViewRun")
        self.pauseAnimate()
        self.resumeAnimate()
    }

    func stop() {
        self.back?.removeFromSuperview()
        back = UIView.init(frame: self.bounds)
        addSubview(back)
        var nums: [Double] = []
        for _ in 0..<Int(self.lgf_Width) {
            nums.append(0.0)
        }
        self.add_back_view(nums, CGRect.init(x: 0, y: 0, w: self.lgf_Width, h: self.lgf_Height))
    }
    
    func showLine(_ numStrs: [String]) {
        var nums: [Double] = []
        
        if numStrs.count == 0 {
            for _ in 0..<Int(self.lgf_Width) {
                nums.append(0.0)
            }
        } else {
            for (i, v) in numStrs.enumerated() {
                if i % 40 == 0 {// 取余 40
                    let num = EWEFilter.getComplement(v)
                    nums.append(num)
                }
            }
        }
        self.add_back_view(nums, CGRect.init(x: self.back.lgf_Width, y: 0, w: self.lgf_Width / 10.0, h: self.lgf_Height))
        self.back.lgf_Width += self.lgf_Width / 10.0
    }
    
    override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()!
        context.setStrokeColor(UIColor.init(lgf_HexString: "f0f0f0").cgColor)
        context.setLineWidth(1.0)
        context.move(to: CGPoint(x: rect.width / 50.0, y: 0))
        for i in 1..<50 {
            let points:[CGPoint] = [CGPoint.init(x: CGFloat(i) * rect.width / 50.0, y: 0), CGPoint.init(x: CGFloat(i) * rect.width / 50.0, y: rect.height)]
            context.addLines(between: points)
            context.strokePath()
        }
        for i in 1..<25 {
            let points:[CGPoint] = [CGPoint.init(x: 0, y: CGFloat(i) * rect.height / 25.0), CGPoint.init(x: rect.width, y: CGFloat(i) * rect.height / 25.0)]
            context.addLines(between: points)
            context.strokePath()
        }
    }
    
}

class LineBackView: UIView {
    
    var nums: [Double] = []
    var count: Int = 0
    
    deinit {
        debugPrint("LineBackView 已删除")
    }
    
    func show(_ nums: [Double]) {
        self.nums.removeAll()
        self.nums = nums
        self.layer.setNeedsDisplay()
    }
    
    override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()!
        context.setStrokeColor(UIColor.init(lgf_HexString: "0F88F2").cgColor)
        context.setLineWidth(1.0)
        var points:[CGPoint] = []
        if let sv = self.superview?.superview as? EWELineView {
            if sv.lastPoint != nil {
                context.move(to: sv.lastPoint)
                points.append(sv.lastPoint)
            }
            if self.nums.count > 0 {
                let apoint = CGPoint(x: 0, y: (rect.height / 2) - (CGFloat(self.nums.last!) * (rect.height / 2)))
                sv.lastPoint = apoint
            }
        } else {
            context.move(to: CGPoint.init(x: 0, y: rect.height / 2))
        }
        for (i, value) in self.nums.enumerated() {
            let apoint = CGPoint(x: CGFloat(i + 1) * rect.width / CGFloat(self.nums.count), y: (rect.height / 2) - (CGFloat(value) * (rect.height / 2)))
            points.append(apoint)
        }
        context.addLines(between: points)
        context.strokePath()
    }

}

class EWELineScrollView: UIView {
    
    var scroll_back: UIScrollView!
    
    deinit {
        debugPrint("EWELineScrollView 已删除")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        scroll_back = UIScrollView.init(frame: self.bounds)
        scroll_back.backgroundColor = UIColor.clear
        scroll_back.showsHorizontalScrollIndicator = false
        addSubview(scroll_back)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(_ rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()!
        context.setStrokeColor(UIColor.init(lgf_HexString: "f0f0f0").cgColor)
        context.setLineWidth(1.0)
        context.move(to: CGPoint(x: rect.width / 50.0, y: 0))
        for i in 1..<50 {
            let points:[CGPoint] = [CGPoint.init(x: CGFloat(i) * rect.width / 50.0, y: 0), CGPoint.init(x: CGFloat(i) * rect.width / 50.0, y: rect.height)]
            context.addLines(between: points)
            context.strokePath()
        }
        for i in 1..<25 {
            let points:[CGPoint] = [CGPoint.init(x: 0, y: CGFloat(i) * rect.height / 25.0), CGPoint.init(x: rect.width, y: CGFloat(i) * rect.height / 25.0)]
            context.addLines(between: points)
            context.strokePath()
        }
    }
    
}

class LineBackView2: UIView {
    
    var nums: [Double] = []
    var ybs: Int = 1
    var count: Int = 0
    var lineColor: UIColor = UIColor.init(lgf_HexString: "65CA33")
    
    deinit {
        debugPrint("LineBackView 已删除")
    }
    
    func show(_ nums: [Double], _ ybs: Int) {
        self.nums.removeAll()
        self.ybs = ybs
        self.nums = nums
        self.layer.setNeedsDisplay()
    }
    
    override func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            context.setStrokeColor(lineColor.cgColor)
            context.setLineWidth(1.0)
            var points:[CGPoint] = []
            if let sv = self.superview {
                if sv.lgf_Name != nil {
                    let apoint = CGPoint(x: 0, y: (rect.height / 2) - (CGFloat(Double(sv.lgf_Name!)!) * (rect.height / 2)))
                    context.move(to: apoint)
                    points.append(apoint)
                }
                if self.nums.count > 0 {
                    sv.lgf_Name = self.nums.last!.description
                }
            } else {
                context.move(to: CGPoint.init(x: 0, y: rect.height / 2))
            }
            for (i, value) in self.nums.enumerated() {
                let apoint = CGPoint(x: CGFloat(i + 1) * rect.width / CGFloat(self.nums.count), y: (rect.height / 2) - (CGFloat(value * Double(self.ybs)) * (rect.height / 2)))
                points.append(apoint)
            }
            context.addLines(between: points)
            context.strokePath()
            
        }
    }

}
